<?php
	class Question extends MY_Controller{
		
		public function __construct(){
			parent::__construct();
			$this->load->model('question_model');
			$this->load->model('trivia_model');
		}
		
		public function index($limit=FALSE){
			$this->load->helper("form");
			//set the limit or offset for each page
			$this->question_model->pager($limit);
			
			// define the link for pagination
			$my_link = site_url('question');		
		
			$data['question'] = $this->question_model->get_question();
			
			// call the pagination from parent controller			
			$this->pager($my_link, $this->question_model->get_total(), $this->question_model->get_record_per_page());
			
			$data['trivia'] = $this->trivia_model->get_trivia_list();
			
			$data['title'] = "Question list";
			$this->load->view('templates/header1', $data);
			$this->load->view('question/index', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');			
		}
		
		public function filter($g_id=FALSE, $limit=FALSE){
			$this->load->helper("form");
			//set the limit or offset for each page
			$this->question_model->pager($limit);
				
			// define the link for pagination
			$my_link = site_url('question/filter/'.$g_id);				
			
			$data['question'] = $this->question_model->get_question(FALSE, $g_id);
			
			// call the pagination from parent controller			
			$this->pager($my_link, $this->question_model->get_total_filter($g_id), $this->question_model->get_record_per_page(), 4);
						
			$data['g_id'] = $g_id;
			$data['title'] = "Question list";
			$this->load->view('templates/header1', $data);
			$this->load->view('question/index', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');
		}
		
		public function search($src=FALSE,$limit=FALSE){
			$this->load->helper("form");
			$data['title'] = "Question list";
			$search_text = '';
			
			//set the limit or offset for each page
			$this->question_model->pager($limit);			
			
			if($src!=FALSE){				
				$data['question'] = $this->question_model->search($src);		
				$search_text = $src;
			}
			else{
				$search_text = $this->input->post('search_text');				
				$data['question'] = $this->question_model->search($search_text);		
			}
			
			// define the link for pagination
			$my_link = site_url('question/search/'.$search_text);
			
			// call the pagination from parent controller get_total_search
			$this->pager($my_link, $this->question_model->get_total_search($search_text), $this->question_model->get_record_per_page(), 4);
			
			$data['trivia'] = $this->trivia_model->get_trivia_list();
			
			$this->load->view('templates/header1', $data);
			$this->load->view('question/index', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');
		}
		
		public function displayForm($id=FALSE){			
				$this->load->helper("form");
				$data = array();
				if($id!=FALSE){
					$data['question'] = $this->question_model->get_question($id);				
				}				
				$data['title']="Create Question";
				$data['trivia'] = $this->trivia_model->get_trivia_list();
				$this->load->view('templates/header1', $data);
				$this->load->view('question/form', $data);
				$this->load->view('templates/right', $data);
				$this->load->view('templates/footer');
			
		}
		
		public function create(){
			$this->displayForm();
		}
		
		public function edit($id){
			$this->displayForm($id);
		}
		
		public function delete($id){
			$this->question_model->delete($id);
			redirect("question", "refresh");
		}
		
		public function save(){			
			
			$this->load->helper("form");
			$this->load->library('form_validation');
			$this->form_validation->set_rules('question', 'Question', 'required');
			if ($this->form_validation->run() === FALSE){
				$data['trivia'] = $this->trivia_model->get_trivia_list();
				$id = $this->input->post('id');
				if(!empty($id))
					$data['question'] = $this->question_model->get_question($id);	
				$data['title']="Create Question";
				$this->load->view('templates/header1', $data);
				$this->load->view('question/form', $data);
				$this->load->view('templates/right', $data);
				$this->load->view('templates/footer');
			}
			else{
				$this->question_model->save();
				redirect("question", "refresh");
			}			
		}	
		
	}
?>